package org.jeecg.modules.demo.com.kunpeng.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.demo.com.kunpeng.entity.CustomerInformation;
import org.jeecg.modules.demo.com.kunpeng.entity.PurchaseContract;
import org.jeecg.modules.demo.com.kunpeng.entity.PurchaseContractDetail;
import org.jeecg.modules.demo.com.kunpeng.entity.PurchaseContractCollectionPlan;
import org.jeecg.modules.demo.com.kunpeng.entity.dto.PurchaseContractKeyWordsQueryDTO;
import org.jeecg.modules.demo.com.kunpeng.mapper.PurchaseContractDetailMapper;
import org.jeecg.modules.demo.com.kunpeng.mapper.PurchaseContractCollectionPlanMapper;
import org.jeecg.modules.demo.com.kunpeng.mapper.PurchaseContractMapper;
import org.jeecg.modules.demo.com.kunpeng.service.ICustomerInformationService;
import org.jeecg.modules.demo.com.kunpeng.service.IPurchaseContractCollectionPlanService;
import org.jeecg.modules.demo.com.kunpeng.service.IPurchaseContractDetailService;
import org.jeecg.modules.demo.com.kunpeng.service.IPurchaseContractService;
import org.jeecg.modules.demo.com.kunpeng.vo.CustomerInformationPage;
import org.jeecg.modules.demo.com.kunpeng.vo.PurchaseContractVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 采购合同表
 * @Author: jeecg-boot
 * @Date:   2022-12-23
 * @Version: V1.0
 */
@Service
@Slf4j
public class PurchaseContractServiceImpl extends ServiceImpl<PurchaseContractMapper, PurchaseContract> implements IPurchaseContractService {

	@Autowired
	private PurchaseContractMapper purchaseContractMapper;
	@Autowired
	private PurchaseContractDetailMapper purchaseContractDetailMapper;
	@Autowired
	private PurchaseContractCollectionPlanMapper purchaseContractCollectionPlanMapper;
	@Autowired
	private IPurchaseContractDetailService purchaseContractDetailService;
	@Autowired
	private IPurchaseContractCollectionPlanService purchaseContractCollectionPlanService;
	@Autowired
	private ICustomerInformationService customerInformationService;

	/**
	 * 采购表添加
	 * @param purchaseContract
	 * @param purchaseContractDetailList
	 * @param purchaseContractCollectionPlanList
	 * @param customerInformation
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(PurchaseContract purchaseContract, List<PurchaseContractDetail> purchaseContractDetailList, List<PurchaseContractCollectionPlan> purchaseContractCollectionPlanList, CustomerInformation customerInformation) {
		purchaseContract.setStatus("0");
		purchaseContractMapper.insert(purchaseContract);
		log.info("采购合同表数据插入成功！");
		customerInformation.setMainId(purchaseContract.getId());
		CustomerInformationPage customerInformationPage=new CustomerInformationPage();
		BeanUtils.copyProperties(customerInformation,customerInformationPage);
		customerInformationService.insert(customerInformationPage);
		log.info("客户表数据插入成功！");
		if(purchaseContractDetailList!=null && purchaseContractDetailList.size()>0) {
			for(PurchaseContractDetail entity:purchaseContractDetailList) {
				//外键设置
				entity.setScid(purchaseContract.getId());
				purchaseContractDetailMapper.insert(entity);
				log.info("采购详情表数据插入成功！");
			}
		}
		if(purchaseContractCollectionPlanList!=null && purchaseContractCollectionPlanList.size()>0) {
			for(PurchaseContractCollectionPlan entity:purchaseContractCollectionPlanList) {
				//外键设置
				entity.setScid(purchaseContract.getId());
				purchaseContractCollectionPlanMapper.insert(entity);
				log.info("采购合同收款计划表数据插入成功！");
			}
		}
	}

	/**
	 * 采购合同表编辑
	 * @param purchaseContract
	 * @param purchaseContractDetailList
	 * @param purchaseContractCollectionPlanList
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(PurchaseContract purchaseContract,List<PurchaseContractDetail> purchaseContractDetailList,List<PurchaseContractCollectionPlan> purchaseContractCollectionPlanList) {
		purchaseContractMapper.updateById(purchaseContract);
		
		//1.先删除子表数据
		purchaseContractDetailMapper.deleteByMainId(purchaseContract.getId());
		purchaseContractCollectionPlanMapper.deleteByMainId(purchaseContract.getId());
		
		//2.子表数据重新插入
		if(purchaseContractDetailList!=null && purchaseContractDetailList.size()>0) {
			for(PurchaseContractDetail entity:purchaseContractDetailList) {
				//外键设置
				entity.setScid(purchaseContract.getId());
				purchaseContractDetailMapper.insert(entity);
			}
		}
		if(purchaseContractCollectionPlanList!=null && purchaseContractCollectionPlanList.size()>0) {
			for(PurchaseContractCollectionPlan entity:purchaseContractCollectionPlanList) {
				//外键设置
				entity.setScid(purchaseContract.getId());
				purchaseContractCollectionPlanMapper.insert(entity);
			}
		}
	}

	/**
	 * 根据主表id删除
	 * @param id
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		purchaseContractDetailMapper.deleteByMainId(id);
		purchaseContractCollectionPlanMapper.deleteByMainId(id);
		purchaseContractMapper.deleteById(id);
	}

	/**
	 * 根据主表id批量删除
	 * @param idList
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			purchaseContractDetailMapper.deleteByMainId(id.toString());
			purchaseContractCollectionPlanMapper.deleteByMainId(id.toString());
			purchaseContractMapper.deleteById(id);
		}
	}

	/**
	 * 根据关键字查询数据
	 * @param page
	 * @param purchaseContractKeyWordsQueryDTO
	 * @return
	 */
	@Override
	public Page<PurchaseContractVO> listByKeyWords(Page<PurchaseContractVO> page, PurchaseContractKeyWordsQueryDTO purchaseContractKeyWordsQueryDTO) {
		log.info("开始处理根据关键字查询数据表信息：service");
		Page<PurchaseContractVO> list = page.setRecords(purchaseContractMapper.listByKeyWords(page, purchaseContractKeyWordsQueryDTO.getContractProperties(), purchaseContractKeyWordsQueryDTO.getStatus(),
				purchaseContractKeyWordsQueryDTO.getKeyWords()));
		//遍历采购合同
		for (PurchaseContractVO purchaseContract:list.getRecords()) {
			System.out.println("采购合同数据："+purchaseContract.toString());
			//采购合同不含税金额
			purchaseContract.setNoTaxAmount(purchaseContract.getContractAmount().multiply(new BigDecimal(purchaseContract.getTaxPoint())));
			List<PurchaseContractCollectionPlan> purchaseContractCollectionPlans = purchaseContractCollectionPlanMapper.selectByMainId(purchaseContract.getId());
			if(purchaseContractCollectionPlans.size()>0){
				BigDecimal returnAmount=new BigDecimal(0.00);
				for(PurchaseContractCollectionPlan saleContractCollectionPlan:purchaseContractCollectionPlans){
					System.out.println("采购合同收款计划数据："+saleContractCollectionPlan.toString());
					Long nowTimeMillis = System.currentTimeMillis();
					Calendar cal=Calendar.getInstance();
					cal.setTime(saleContractCollectionPlan.getAgreedCollectionTime());
					Long collectionMillis=cal.getTimeInMillis();
					if(nowTimeMillis>collectionMillis){
						returnAmount=returnAmount.add(saleContractCollectionPlan.getAmountCollected());
					}
				}
				//采购合同回款金额
				purchaseContract.setReturnAmount(returnAmount);
				//采购合同未回款金额
				purchaseContract.setNoReturnAmount(purchaseContract.getNoTaxAmount().subtract(returnAmount));
			}
			/*if (!(purchaseContract.getContractStartTime() == null || purchaseContract.getContractEndTime() == null)) {
				long nowTimeMillis = System.currentTimeMillis();
				Calendar cal = Calendar.getInstance();
				cal.setTime(purchaseContract.getContractStartTime());
				long startTimeMillis = cal.getTimeInMillis();
				cal.setTime(purchaseContract.getContractEndTime());
				Long endTimeMillis = cal.getTimeInMillis();
				Long seTime = endTimeMillis - startTimeMillis;
				Long snTime = nowTimeMillis - startTimeMillis;
				if (snTime > seTime) {
					purchaseContract.setStatus("已完成");
					purchaseContractMapper.updateStateById(purchaseContract.getId(),"3");
				} else if (startTimeMillis > nowTimeMillis) {
					purchaseContract.setStatus("未开始");
					purchaseContractMapper.updateStateById(purchaseContract.getId(), "1");
				} else if (seTime > snTime) {
					purchaseContract.setStatus("执行中");
					purchaseContractMapper.updateStateById(purchaseContract.getId(), "2");
				}
			}*/
			String status = purchaseContract.getStatus();
			switch (status){
				case "0":purchaseContract.setStatus("待审批");
					break;
				case "1":purchaseContract.setStatus("已审批");
					break;
				case "2":purchaseContract.setStatus("中止");
					break;
				case "3":purchaseContract.setStatus("作废");
					break;
			}
		}
		try {
			if(list.getRecords().size()==0);
		} catch (Exception e) {
			Result.error("数据不存在，请重新输入！");
			e.printStackTrace();
		}
		for (PurchaseContractVO purchaseContractVO:list.getRecords()) {
			purchaseContractVO.setPurchaseContractDetailList(purchaseContractDetailService.selectByMainId(purchaseContractVO.getId()));
			purchaseContractVO.setPurchaseContractCollectionPlanList(purchaseContractCollectionPlanService.selectByMainId(purchaseContractVO.getId()));
		}
		return list;
	}

	/**
	 * 根据id修改合同状态
	 * @param id
	 * @param status
	 */
	@Override
	public void updateStateById(String id, String status) {
		purchaseContractMapper.updateStateById(id,status);
	}

}
